package com.vsu.bookstore.mvc.ui.widgetcontainers;

import java.util.HashMap;
import java.util.Map;

import org.vaadin.imagefilter.Image;

import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vsu.bookstore.BookstoreApplication;
import com.vsu.bookstore.mvc.model.bean.BookStoreItem;
import com.vsu.bookstore.mvc.ui.widgets.EuroToRubleLabel;

@SuppressWarnings("serial")
public class CartPanel extends Panel
{
  public static class PreviewLayout extends VerticalLayout
  {
    private BookStoreItem item = null;

    private final Label priceLabel = new EuroToRubleLabel();

    public PreviewLayout(final BookStoreItem bean)
    {
      super();
      addStyleName("floating");
      setWidth("125px");
      setHeight("160px");
      setMargin(true, true, true, false);
      setSpacing(false);
      setItem(bean);
      final Image image = new Image();
      image.load(item.getImage());
      addComponent(image);
      updateLabelText();
      priceLabel.setSizeUndefined();
      addComponent(priceLabel);
      addListener(new LayoutClickListener()
      {
        public void layoutClick(final LayoutClickEvent event)
        {
          if (bean != null)
            ((BookstoreApplication) getApplication()).ui().showDetailed(bean);
        }
      });
      setComponentAlignment(image, Alignment.MIDDLE_CENTER);
      setComponentAlignment(priceLabel, Alignment.BOTTOM_CENTER);
    }

    public BookStoreItem getItem()
    {
      return item;
    }

    public void setItem(final BookStoreItem item)
    {
      this.item = item;
    }

    public void updateLabelText()
    {
      priceLabel.setValue(item);
    }
  }

  private final CssLayout layout = new CssLayout();

  private final Map<Integer, PreviewLayout> previews =
    new HashMap<Integer, PreviewLayout>();

  public CartPanel()
  {
    super();
    addStyleName("light");
    setHeight("100%");
    setContent(layout);
    setCaption("In Your Cart");
    layout.setSizeFull();
  }

  public void removeAllPreviews()
  {
    previews.clear();
    removeAllComponents();
  }

  private void removePreview(final PreviewLayout preview)
  {
    previews.remove(preview.getItem().getId());
    removeComponent(preview);
  }

  public void updateItem(final BookStoreItem element, final boolean isAdding)
  {
    final PreviewLayout image = previews.get(element.getId());
    if (image == null &&
        isAdding)
    {
      final PreviewLayout preview = new PreviewLayout(element);
      previews.put(element.getId(), preview);
      layout.addComponent(preview);
      return;
    }
    final PreviewLayout preview = previews.get(element.getId());
    if (preview == null)
      return;
    if (preview.getItem().getQuantity() == 0)
      removePreview(preview);
    preview.updateLabelText();
  }
}
